<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Main extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this -> load -> library('googlemaps');
		$this -> load -> model('m_pmi');
	}

	public function index() {

		if (!$this -> session -> userdata('validated'))
			redirect('login');

		$page = 'home';
		$data['page'] = $page;
		$this -> load -> view('main', $data);
	}

	public function dologout() {
		$this -> session -> sess_destroy();
		redirect('login');
	}

	public function stock($id = 0) {
		$page = 'stock';
		$data['page'] = $page;
		$data['udd'] = $this -> m_pmi -> udd();
		$data['selected'] = $id;
		$data['stock'] = $this -> m_pmi -> stock($id, 10, 0);
		$this -> load -> view('main', $data);
	}

	public function broadcast() {
		$page = 'broadcast';
		$data['page'] = $page;
		$this -> load -> view('main', $data);
	}

	public function udd() {
		$page = 'udd';
		$data['page'] = $page;
		$data['udd'] = $this -> m_pmi -> udd();
		$this -> load -> view('main', $data);
	}

	public function tambahudd() {

		$config['center'] = '-7.782943296564264, 110.36700578137209';
		$config['zoom'] = 'auto';
		$this -> googlemaps -> initialize($config);

		$marker = array();
		$marker['position'] = '-7.782943296564264, 110.36700578137209';
		$marker['draggable'] = true;
		$marker['ondragend'] = '/* alert(\'You just dropped me at: \' + event.latLng.lat() + \', \' + event.latLng.lng()); */' . ' document.getElementsByName(\'long\').item(0).value= event.latLng.lng() ;' . ' document.getElementsByName(\'lat\').item(0).value= event.latLng.lat() ;';
		$this -> googlemaps -> add_marker($marker);
		$data['map'] = $this -> googlemaps -> create_map();

		$this -> load -> view('tambaheditudd', $data);
	}

	public function editudd($id) {
		$data['udd'] = $this -> m_pmi -> uddbyid($id);

		$config['center'] = $data['udd']['lat'] . ', ' . $data['udd']['long'];
		$config['zoom'] = 'auto';
		$this -> googlemaps -> initialize($config);

		$marker = array();
		$marker['position'] = $data['udd']['lat'] . ', ' . $data['udd']['long'];
		$marker['draggable'] = true;
		$marker['ondragend'] = '/* alert(\'You just dropped me at: \' + event.latLng.lat() + \', \' + event.latLng.lng()); */' . ' document.getElementsByName(\'long\').item(0).value= event.latLng.lng() ;' . ' document.getElementsByName(\'lat\').item(0).value= event.latLng.lat() ;';
		$this -> googlemaps -> add_marker($marker);
		$data['map'] = $this -> googlemaps -> create_map();

		$this -> load -> view('tambaheditudd', $data);
	}

	public function dotambahudd() {
		$post_data = $this -> input -> post();
		if ($this -> m_pmi -> tambahudd($post_data)) {
			$this -> udd();
		} else {
			tambahudd();
		}
		//print_r($post_data);
	}

	public function dotambahstock() {
		$post_data = $this -> input -> post();
		if ($this -> m_pmi -> tambahstock($post_data)) {
			$this -> stock($post_data['id_udd']);
		} else {
			$this -> stock($post_data['id_udd']);
		}
		//print_r($post_data);
	}

	public function dohapusstock($id) {
		if ($this -> m_pmi -> hapusstock($id)) {
			$this -> stock();
		} else {
			$this -> stock();
		}
	}

	public function dohapusudd($id) {
		if ($this -> m_pmi -> hapusudd($id)) {
			$this -> udd();
		} else {
			$this -> udd();
		}
	}

	public function doeditudd() {
		$post_data = $this -> input -> post();

		if ($this -> m_pmi -> editudd($post_data)) {
			$this -> udd();
		} else {
			$this -> udd();
		}
	}

}
